package com.clps.dev.sms.dao.sm.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.clps.dev.sms.common.result.ResultType;
import com.clps.dev.sms.dao.sm.DepartmentDao;
import com.clps.dev.sms.entity.sm.Department;
import com.clps.dev.sms.util.object.ExceptionTool;
import com.clps.dev.sms.util.order.GBKOrder;

/**  
* @ClassName: DepartmentDaoImpl  
* @Description:DepartmentDaoImpl 实现类
* @since JDK 1.8 
* @author RuiShan.Yang  
* @date 2018-11-27 09:59:04  
* 
*/
@Repository
public class DepartmentDaoImpl extends HibernateDaoSupport implements DepartmentDao {
	private static final String HQL_QUERY_BY_DEPARTMENT_NAME = "FROM Department WHERE departmentName LIKE '%'||?||'%'";

	List<Department> departmentList = null;

	private Logger logger = Logger.getLogger(Logger.class);

	@Resource
	private SessionFactory sessionFactory;

	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	@Autowired
	public void setMySessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

	/**
	* <p>Title: save</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.dao.sm.DepartmentDao#save(com.clps.dev.sms.entity.sm.Department)  
	*/
	@Override
	public String save(Department department) {
		try {
			this.getHibernateTemplate().save(department);
			logger.info("save department success");
			return ResultType.ADD_SUCCESS;
		} catch (Exception e) {
			return new ExceptionTool(e).exceptionVal();
		}
	}

	/**  
	* @Title: deleteAll  
	* @Description: 删除子部门
	* @param department void
	*/
	private void deleteAll(Department department) {
		@SuppressWarnings("unchecked")
		List<Department> list = (List<Department>) this.getHibernateTemplate()
				.find("FROM Department WHERE departmentParentId = \'" + department.getDepartmentId() + "\'");
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				deleteAll(list.get(i));
			}
		}
		this.getHibernateTemplate().delete(department);
		
	}

	/**  212 214 222 26 
	* <p>Title: deleteById</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.dao.sm.DepartmentDao#deleteById(java.io.Serializable)  
	*/
	@Override
	public String deleteById(Serializable deaprtmentId) {

		Department department = queryById(deaprtmentId);
		// getSessionFactory().getCurrentSession().clear();
		this.getSession().clear();
		if (null != department) {
			deleteAll(department);			
			Department department2 = queryById(deaprtmentId);
			if (department2 == null) {
				logger.info("delete success");
				return ResultType.DELETE_SUCCESS;
			} else {
				logger.info("delete failure");
				return ResultType.DELETE_FAULT;
			}
		} else {
			logger.info("no data");
			return ResultType.DELETE_FAULT;
		}
	}

	/**
	* <p>Title: update</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.dao.sm.DepartmentDao#update(com.clps.dev.sms.entity.sm.Department, java.io.Serializable)  
	*/
	@Override
	public String update(Department department, Serializable departmentId) {
		Department department2 = queryById(departmentId);
		this.getSession().clear();
		if (null != department2) {
			this.getHibernateTemplate().update(department);
			Department department3 = queryById(departmentId);
			if (department3.equals(department)) {
				logger.info("update success");
				return ResultType.UPDATE_SUCCESS;
			} else {
				logger.info("update failure");
				return ResultType.UPDATE_FAULT;
			}
		} else {
			logger.info("no data");
			return ResultType.UPDATE_FAULT;
		}
	}

	/**
	* <p>Title: queryById</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.dao.sm.DepartmentDao#queryById(java.io.Serializable)  
	*/
	@Override
	public Department queryById(Serializable departmentId) {

		return (Department) this.getHibernateTemplate().get(Department.class, departmentId);
	}

	/**
	* <p>Title: queryALl</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.dao.sm.DepartmentDao#queryALl()  
	*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> queryALl() {
		return (List<Department>) this.getHibernateTemplate().find("FROM " + Department.class.getSimpleName());
	}

	/**
	* <p>Title: queryByField</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.dao.sm.DepartmentDao#queryByField(java.lang.String, java.lang.String)  
	*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> queryByField(String departmentFieldName, String departmentfieldVal) {
		return (List<Department>) this.getHibernateTemplate()
				.find("FROM Department WHERE " + departmentFieldName + " = \'" + departmentfieldVal + "\'");
	}

	/**
	* <p>Title: findCount</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.dao.sm.DepartmentDao#findCount(org.hibernate.criterion.DetachedCriteria)  
	*/
	@SuppressWarnings("unchecked")
	@Override
	public Integer findCount(DetachedCriteria detachedCriteria) {
		detachedCriteria.setProjection(Projections.rowCount());
		List<Long> list = (List<Long>) this.getHibernateTemplate().findByCriteria(detachedCriteria);
		if (list.size() > 0) {
			return list.get(0).intValue();
		}
		return null;
	}

	/**
	* <p>Title: findByPage</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.dao.sm.DepartmentDao#findByPage(org.hibernate.criterion.DetachedCriteria, java.lang.Integer, java.lang.Integer)  
	*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> findByPage(DetachedCriteria detachedCriteria, Integer begin, Integer pageSize) {
		detachedCriteria.setProjection(null);

		return (List<Department>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);
	}

	/**
	* <p>Title: queryBySql</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.dao.sm.DepartmentDao#queryBySql(java.lang.String)  
	*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> queryBySql(String sql) {
		departmentList = getSession().createSQLQuery(sql).list();
		return departmentList;
	}

	/**
	* <p>Title: queryByDepartmentName</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.dao.sm.DepartmentDao#queryByDepartmentLikeName(java.lang.String)  
	*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> queryByDepartmentLikeName(String departmentName) {
		departmentList = (List<Department>) this.getHibernateTemplate().find(HQL_QUERY_BY_DEPARTMENT_NAME,
				departmentName);
		return departmentList;
	}

	/**
	* <p>Title: findByPageOrderByName</p>  
	* <p>Description: </p> 
	* @see com.clps.dev.sms.dao.sm.DepartmentDao#findDepartmentByPageOrderByName(org.hibernate.criterion.DetachedCriteria, java.lang.Integer, java.lang.Integer, java.lang.String)  
	*/
	@SuppressWarnings("unchecked")
	@Override
	public List<Department> findDepartmentByPageOrderByName(DetachedCriteria detachedCriteria, Integer begin,
			Integer pageSize, String departmentName) {
		detachedCriteria.setProjection(null);
		detachedCriteria.addOrder(GBKOrder.asc(departmentName));
		return (List<Department>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> findDepartmentByPageOrderByTime(DetachedCriteria detachedCriteria, Integer begin,
			Integer pageSize, String departmentCreatTime) {
		detachedCriteria.setProjection(null);
		detachedCriteria.addOrder(Order.asc(departmentCreatTime));
		return (List<Department>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);

	}

}
